<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Master_barang extends CI_Controller {

	function __construct() {
		parent::__construct();

		//jika digunakan di mayoritas function, panggil disini
		//model
		$this->load->model('m_master_barang');

		//library breadcrum/untuk navigasi
		$this->load->library('breadcrumb');		
	}

	function index() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//paging dan ambil data dari model
		$this->load->library('paging');                         		
		//Load library "paging"
		$config['sql']      				= $this->m_master_barang->get();
    	//Query SQL - bukan array lho ya, liat model
		$config['per_page'] 				= 15;                            
    	//jumlah item tiap halaman
		$data = $this->paging->create_pagination($config);   				
    	//Load fungsi di library "paging" untuk membuat pagination. 

		//menghilangkan session jika ada
		$this->session->unset_userdata('keyword');

		//judul halaman
		$judul                  = 'Barang';
		$data['judul']  = $judul;
		
    // Title HTML
		$this->layout->set_title($judul);
    //meta description jika perlu
		$this->layout->set_desc('Ini hanya contoh');

		//css tambahan bila perlu
		$this->layout->set_include('css', 'themes/apotek/css/path-to-file.css');
		//js tambahan bila perlu (js otomatis footer)
		$this->layout->set_include('js', 'themes/apotek/css/path-to-fileAAA.js');
		//css tambahan bila perlu (js di header)
		$this->layout->set_include('js', 'themes/apotek/css/path-to-file.js', 'header');

    //breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Contoh', site_url('master_barang#'));
		$this->breadcrumb->add_crumb($judul);

    //untuk motong kalimat
		$this->load->helper('trim_text');

		//menggunakan theme back
		$this->layout->theme('apotek','master_barang/index', $data);
	}

	function cari() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//buat cari
		if(isset($_POST['keyword'])){
			$data['keyword'] = $this->input->post('keyword');
			$this->session->set_userdata('keyword', $data['keyword']);
		} else {
			$data['keyword'] = $this->session->userdata('keyword');
		}	

		//paging dan ambil data dari model
		$this->load->library('paging');                         		//Load the "paging" library
    $config['sql']      				= $this->m_master_barang->get_cari($data['keyword']);//your SQL - bukan array lho ya...
    $config['per_page'] 				= 5;                            //Display items per page
    $data = $this->paging->create_pagination($config);   				//Load function in "paging" libraryfor create pagination. 

    //judul halaman
    $judul                  = 'Pencarian barang " <font color="#FC771F">' . $this->session->userdata('keyword') . '</font> "';
    $data['judul']  = $judul;
    
    // Title HTML
    $this->layout->set_title($judul);
    //meta description jika perlu
    $this->layout->set_desc('Made with love by Raksa Abadi Informatika');

		//css tambahan bila perlu
    $this->layout->set_include('css', 'themes/apotek/css/path-to-file.css');
		//js tambahan bila perlu (js otomatis footer)
    $this->layout->set_include('js', 'themes/apotek/css/path-to-file.js');
		//css tambahan bila perlu (js di header)
    $this->layout->set_include('js', 'themes/apotek/css/path-to-file.js', 'header');

		//breadcrumb/untuk navigasi
    $this->breadcrumb->clear();
    $this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
    $this->breadcrumb->add_crumb('Contoh', site_url('master_barang#'));
    $this->breadcrumb->add_crumb('Barang', site_url('master_barang'));
    $this->breadcrumb->add_crumb($judul);

    //untuk motong kalimat
    $this->load->helper('trim_text');

		//menggunakan theme back
    $this->layout->theme('apotek','master_barang/index', $data);
  }

  function add() {

		//rule type pada function ini: Create
  	$this->rule->type('C');

  	if (isset($_POST) && !empty($_POST)) {

			//ambil data dari form
  		$dataInsert = array(
  			'id_produksi' => $this->input->post('id_produksi'), 
  			'nama' => $this->input->post('nama'), 
  			'ket' => $this->input->post('ket')
  			);

  		$this->m_master_barang->save($dataInsert);
  		redirect('master_barang','refresh');
  	}else{

			//judul halaman
  		$judul                  = 'Tambah Barang';
  		$data['judul']  = $judul;

	    // Title HTML
  		$this->layout->set_title($judul);
	    //meta description jika perlu
  		$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

			//js tambahan
  		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
  		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
  		$this->layout->set_include('js', 'themes/general/bundle/ckeditor/ckeditor.js', 'header');

	    //breadcrumb/untuk navigasi
  		$this->breadcrumb->clear();
  		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
  		$this->breadcrumb->add_crumb('Contoh', site_url('master_barang#'));
  		$this->breadcrumb->add_crumb('Barang', site_url('master_barang'));
  		$this->breadcrumb->add_crumb($judul);

			//menggunakan theme back
  		$this->layout->theme('apotek','master_barang/add', $data);

  	}

  }

  function edit($id_b = '', $id_p = '') {

		//rule type pada function ini: Update
  	$this->rule->type('U');

  	if (isset($_POST) && !empty($_POST)) {

      //ambil data dari form
  		$id_barang = $this->input->post('id_barang');
  		$dataUpdate = array(
  			'id_produksi' => $this->input->post('id_produksi'), 
  			'nama' => $this->input->post('nama'), 
  			'ket' => $this->input->post('ket')
  			);

  		$this->m_master_barang->update($id_barang, $dataUpdate);      
  		redirect('master_barang','refresh');

  	}else{

      // Helper untuk mengecek apakan id ada dalam tabel yang dimaksud
      // cek_id(id,field,tabel)
      // return boolean
  		$this->load->helper('cek_id');
  		if(!cek_id($id_b, 'id_barang', 'master_barang') OR !cek_id($id_p, 'id_produksi', 'master_barang')){
  			show_404();
  		}

      //judul halaman
  		$judul                  = 'Edit Barang';
  		$data['judul']  = $judul;

      // Title HTML
  		$this->layout->set_title($judul);
      //meta description jika perlu
  		$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

      //js tambahan
  		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
  		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
  		$this->layout->set_include('js', 'themes/general/bundle/ckeditor/ckeditor.js', 'header');

      //breadcrumb/untuk navigasi
  		$this->breadcrumb->clear();
  		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
  		$this->breadcrumb->add_crumb('Contoh', site_url('master_barang#'));
  		$this->breadcrumb->add_crumb('Barang', site_url('master_barang'));
  		$this->breadcrumb->add_crumb($judul);

      //ambil data dari model dengan berdasarkan parameter diatas
  		$data['data'] = $this->m_master_barang->get_by(array($id_b, $id_p));

      //menggunakan theme back
  		$this->layout->theme('apotek','master_barang/edit', $data);

  	}
  }

  function delete($id_barang = '') {

		//rule type pada function ini: Delete
  	$this->rule->type('D');

    // Helper untuk mengecek apakan id ada dalam tabel yang dimaksud
    // cek_id(id,field,tabel)
    // return boolean
  	$this->load->helper('cek_id');

  	if(!cek_id($id_barang, 'id_barang', 'master_barang')){
  		set_flash('danger','Ups, barang yang diminta tidak tersedia');
  		redirect('master_barang','refresh');
  	}

  	$this->m_master_barang->delete($id_barang);
  	redirect($_SERVER['HTTP_REFERER'],'refresh');

  }

  function excel(){
  	$this->rule->type('R');
		//load our new PHPExcel library
  	$this->load->library('excel');
		//activate worksheet number 1
  	$this->excel->setActiveSheetIndex(0);
		//name the worksheet
  	$this->excel->getActiveSheet()->setTitle('Data Barang');
		//set cell A1 content with some text
  	$this->excel->getActiveSheet()->setCellValue('A1', 'Data Barang Raksa Abadi Informatika');
  	$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
  	$this->excel->getActiveSheet()->setCellValue('B2', 'ID Produksi');
  	$this->excel->getActiveSheet()->setCellValue('C2', 'Nama Barang');
  	$this->excel->getActiveSheet()->setCellValue('D2', 'Keterangan');
		//change the font size
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		//make the font become bold
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
  	$this->excel->getActiveSheet()->mergeCells('A1:D1');
		//set aligment to center for that merged cell (A1 to D1)
  	$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		//set style
  	$TitleStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '00994C') // hijau tua
            ),
  		'font'  => array(
  			'bold'  => false,
  			'color' => array('rgb' => 'FFFFFF'),
  			'size'  => 15,
  			'name'  => 'Verdana'
  			)
  		);

  	$HeaderStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '32CD32') // hijau
            )
  		);

  	$ContentStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFFF00') // kuning
            )
  		);

  	$this->excel->getActiveSheet()->getStyle('A1:D1')->applyFromArray($TitleStyle);
  	$this->excel->getActiveSheet()->getStyle('A2:D2')->applyFromArray($HeaderStyle);
		//isi
  	$list_data = $this->m_master_barang->get_export();
  	$cols = 3;
  	$num = 1;
  	foreach($list_data as $row){
  		$col = $cols++;
  		$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
  		$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['id_produksi']);
  		$this->excel->getActiveSheet()->setCellValue('C' . $col, $row['nama']);
  		$this->excel->getActiveSheet()->setCellValue('D' . $col, $row['ket']);
  	}

		//warna
  	$cols2 = 3;
  	foreach($list_data as $row){
  		$col2 = $cols2++;
  		$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('C' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('D' . $col2)->applyFromArray($ContentStyle);
  	}

  	$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);


		$filename='data barang.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

	function pdf(){
		// rule type
		$this->rule->type('R');
		//ambil data
		$data['results'] = $this->m_master_barang->get_export();
		// pdf library
		$this->load->library('pdf');
		ob_start();		
		$this->load->view('apotek/layouts/master_barang/pdf', $data);
		$content = ob_get_clean();
		try{
  		// L => Landscape, P => Potrait, array(215,330) => ukuran kertas, array(10, 10, 5, 5) => margin
			$html2pdf = new HTML2PDF('P', array(215,330), 'en', true, 'UTF-8', array(10, 10, 5, 5));
			$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
			$html2pdf->Output("data_barang.pdf");
		}
		catch(HTML2PDF_exception $e) {
			echo $e;
			exit;
		}
	}

  public function tampil()
  {
    $data['results'] = $this->m_master_barang->get_export();
    $this->load->view('apotek/layouts/master_barang/pdf', $data);
  }

}
